/**
 * 危险作业审批，确定有列表查询的动作。操作中的“审批”需要根据id查表
 */

package com.xidian.zhiancloud.controller.dangerWorkTicket.workApprove;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.UnsafeWorkApprove;
import com.xidian.zhiancloud.model.workSupervise.ReviewWorkBefore;
import com.xidian.zhiancloud.model.dangerWorkTicket.WorkTicket;
import com.xidian.zhiancloud.qo.dangerWorkTicket.WorkTicketQO;
import com.xidian.zhiancloud.service.dangerWorkTicket.ReviewWorkBeforeService;
import com.xidian.zhiancloud.service.dangerWorkTicket.WorkApprove.UnsafeWorkApproveService;
import com.xidian.zhiancloud.service.dangerWorkTicket.WorkTicket.WorkTicketService;
import com.xidian.zhiancloud.vo.dangerWorkTicket.WorkTicketVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * 第三页作业审批 - 危险作业审批，有查看和修改两个操作，修改仅仅添加isPass和reason两个字段。
 * 查看需要根据id返回 作业票、审批意见两张表，修改需要操作“审批记录表”
 */

@RequestMapping("UnsafeWorkApprove")
@RestController
public class UnsafeWorkApproveController extends BaseController {

    @Autowired
    private UnsafeWorkApproveService unsafeWorkApproveService;

    @Autowired
    private WorkTicketService workTicketService;

    @Autowired
    private ReviewWorkBeforeService reviewWorkBeforeService;

    /**
     * 根据条件查询作业票
     *
     * @param  workTicketQO 查询参数
     * @return 作业票列表
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<WorkTicketVO>> getListByQueryObject(WorkTicketQO workTicketQO){
        if(workTicketQO.getCurrent() == null || workTicketQO.getSize() == null){
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        workTicketQO.setCompanyId(getSessionUserCompanyId());
        /**
         * 工作流，需要判断该用户 是否拥有 某其他用户的作业票申请。写在XML文件中
         */
        return ResultDTO.successOf(unsafeWorkApproveService.getListByQO(workTicketQO), GeneralResponseEnums.SUCCESS);
    }


    /**
     * 审批：显示作业票的部分信息，并显示审批信息（GetMapping）。并在后续进行审批及提交审批意见
     *
     * @param id
     * @param approveResult
     * @param approveView
     * @return
     */
    @PostMapping("/approveUnsafeWork")
    public ResultDTO<Boolean> approveUnsafeWork(@RequestParam("id") Long id,
                                                @RequestParam("approveResult") Integer approveResult,
                                                @RequestParam("approveView") String approveView) {

        // 向t_unsafe_work_approve中新加记录
        UnsafeWorkApprove unsafeWorkApprove = new UnsafeWorkApprove();
        unsafeWorkApprove.setCompanyId(getSessionUserCompanyId());
        unsafeWorkApprove.setApproverId(getSessionUserid());
        unsafeWorkApprove.setRelateWorkId(id);
        unsafeWorkApprove.setApproveTime(new Date(System.currentTimeMillis()));
        unsafeWorkApprove.setApproveResult(approveResult);
        unsafeWorkApprove.setApproveView(approveView);
        // 获取部门id
        unsafeWorkApprove.setApproveOrganizationId(Long.parseLong(getSessionUser().getOrgids().split(",")[0]));

        // 更改作业票状态
        WorkTicket workTicket = workTicketService.getById(id);

        if (workTicket == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        // 新建作业票，为审批中
        workTicket.setWorkStatus(2);
        workTicketService.updateById(workTicket);
        return ResultDTO.successOf(unsafeWorkApproveService.save(unsafeWorkApprove), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id查找t_unsafe_work_approve，展示相关信息
     * 根据t_work_ticket的id获取记录，并显示关键信息
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public ResultDTO<Map<String, Integer>> getById(@RequestParam("id") Long id) {
        ReviewWorkBefore reviewWorkBefore = reviewWorkBeforeService.getById(id);
        if (reviewWorkBefore == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        } else{
            Map<String, Integer> res = reviewWorkBeforeService.getRiskAssess(reviewWorkBefore);
            return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
        }
    }


}
